package com.example.rbacmanage.controller.app;

import com.example.rbacmanage.common.Result;
import com.example.rbacmanage.entity.app.AppMenu;
import com.example.rbacmanage.entity.system.DictData;
import com.example.rbacmanage.entity.system.DictType;
import com.example.rbacmanage.mapper.app.AppMenuMapper;
import com.example.rbacmanage.service.app.AppMenuService;
import com.example.rbacmanage.service.system.DictDataService;
import com.example.rbacmanage.service.system.DictTypeService;
import com.example.rbacmanage.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/app/appmenu")
public class AppMenuAppController {
    @Autowired
    private AppMenuService appMenuService;
    @Autowired
    private DictTypeService dictTypeService;
    @Autowired
    private DictDataService dictDataService;
    @Autowired
    private AppMenuMapper appMenuMapper;
    @GetMapping("/page")
    public Result page(AppMenu appMenu){
        return Result.success(appMenuService.page(appMenu));
    }

    @GetMapping("/list")
    public Result list(AppMenu appMenu){
        return Result.success(appMenuService.getAll(appMenu));
    }


    @GetMapping("/tree")
    public Result tree(AppMenu appMenu){

        DictType dictType = new DictType();
        dictType.setTypeSign("classic_value");
        List<DictType> dictTypes = dictTypeService.getAll(dictType);
        List<MenuVo> list1=new ArrayList<>();
        if(!dictTypes.isEmpty()){
            DictData data = new DictData();
            data.setDictTypeId(dictTypes.get(0).getId());
            List<DictData> list = dictDataService.getAll(data);
            for (DictData dictData : list) {
                MenuVo resp = new MenuVo();
                resp.setName(dictData.getDataName());
                AppMenu appMenu1 = new AppMenu();
                appMenu1.setShopId(appMenu.getShopId());
                appMenu1.setClassicValue(dictData.getDataValue());
                List<AppMenu> menus = appMenuMapper.list(appMenu1);
                resp.setChildrens(menus);
                list1.add(resp);
            }
        }
        return Result.success(list1);
    }
}
